package com.roy.sms.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.roy.sms.dao.ColumnDao;
import com.roy.sms.model.Column;
import com.roy.sms.util.StringUtil;
import com.roy.sms.vo.ColumnVO;
import com.roy.sms.vo.TreeNodeVO;

@Service
public class ColumnService {

	@Resource
	private ColumnDao columnDao;

	@Transactional
	public void add(ColumnVO columnVO) {
		Column column = columnVO.getColumn();
		if(columnVO.getPid() > 0){
			Column p = columnDao.load(columnVO.getPid());
			column.setParentColumn(p);
		}
		columnDao.add(column);
	}

	@Transactional
	public void update(ColumnVO columnVO) {
		columnDao.update(columnVO.getColumn());
	}

	@Transactional
	public void delete(String ids) {
		if (!StringUtil.isEmpty(ids)) {
			String[] sid = ids.split(",");
			for (String id : sid) {
				columnDao.delete(Integer.valueOf(id));
			}
		}
	}

	@Transactional
	public ColumnVO load(int id) {
		return new ColumnVO(columnDao.load(id));
	}

	@Transactional
	public List<ColumnVO> list(int pid) {
		return getColumnVOs(getColumns(pid));
	}
	
	@Transactional
	public List<Column> getColumns(int pid){
		List<Column> columns;
		if(pid == -1){
			columns = columnDao.listTop(Column.TYPE_FRANT);
		} else if(pid == -2){
			columns = columnDao.listTop(Column.TYPE_BACK);
		} else {
			columns = columnDao.listChildren(columnDao.load(pid));
		}
		return columns;
	}

	private List<ColumnVO> getColumnVOs(List<Column> columns) {
		List<ColumnVO> columnVOs = new ArrayList<ColumnVO>();
		for (Column c : columns) {
			columnVOs.add(new ColumnVO(c));
		}
		return columnVOs;
	}
	
	@Transactional
	public List<TreeNodeVO> findColumnTree(int type){
		List<TreeNodeVO> treeNodeVOs = new ArrayList<TreeNodeVO>();
		
		if(type == 1 || type == 3){//前台或全部
			List<Column> frantColumns = columnDao.listTop(Column.TYPE_FRANT);
			TreeNodeVO frant = new TreeNodeVO();
			frant.setId(-1);
			frant.setText("前台");
			frant.setChildren(getTreeNodes(frantColumns));
			treeNodeVOs.add(frant);
		}
		
		if(type == 2 || type == 3){//后台或全部
			List<Column> backColumns = columnDao.listTop(Column.TYPE_BACK);
			TreeNodeVO back = new TreeNodeVO();
			back.setId(-2);
			back.setText("后台");
			back.setChildren(getTreeNodes(backColumns));
			treeNodeVOs.add(back);
		}
		return treeNodeVOs;
	}
	
	private List<TreeNodeVO> getTreeNodes(List<Column> columns){
		List<TreeNodeVO> treeNodeVOs = new ArrayList<TreeNodeVO>();
		for(Column column : columns){
			TreeNodeVO nodeVO = new TreeNodeVO();
			nodeVO.setId(column.getId());
			nodeVO.setText(column.getName());
			List<Column> children = columnDao.listChildren(column);
			if(children != null && children.size() > 0){
				nodeVO.setChildren(getTreeNodes(children));
			}
			
			treeNodeVOs.add(nodeVO);
		}
		return treeNodeVOs;
	}
	
	@Transactional
	public List<TreeNodeVO> getArticleColumnTree(){
		List<Column> frantColumns = columnDao.listTop(Column.TYPE_FRANT);
		return getTreeNodes(frantColumns);
	}
	
}
